
package io;
import java.io.IOException;
import java.io.InputStream;


import exceptions.ReadStreamException;



/**
 * Composes {@link InputStream} interface realization with additional ability to get access to a previous read symbol
 */
public class Reader{
    private int prevSymbol = 0;
    private int curSymbol = 0;
    private InputStream is;
    /**
     * Creates a new Reader object with an {@link InputStream} realization object
     * @param is - input stream, that is the basement of the new object
     */
    public Reader(InputStream is){
        this.is = is;
    }
    /**
     * Gets the next symbol in the stream.
     * @return code of the read symbol
     * @throws ReadStreamException if any of {@link IOException} exceptions happened during the read process
     */
    public int readNext() throws ReadStreamException{
        try{
            prevSymbol = curSymbol;
            curSymbol = is.read();
            return curSymbol;
        }
        catch(IOException e){
            throw new ReadStreamException(e.getMessage());
        }
        finally{
            //empty
        }
    }
    /**
     * @return previous read symbol in the input stream, corresponding current Reader object
     */
    public int getPrev(){
        return prevSymbol;
    }
}
